package com.tencent.examples.core.temp;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tencent.bk.utils.blueking.properties.ApiUrlProperties;
import com.tencent.bk.utils.blueking.tools.HttpsClient;
import com.tencent.bk.utils.blueking.tools.excel.ConstantKeyFilePath;
import com.tencent.bk.utils.blueking.tools.excel.FileUtils;
import com.tencent.bk.utils.blueking.tools.excel.ImportExcelUtil;
import com.tencent.examples.core.processor.common.CommParams;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/web/admin/import")
public class ImportAction {
    @Autowired
    CommParams commParams;
    @Autowired
    ApiUrlProperties apiUrlProperties;

    /**
     * 批量导入
     *
     * @return1
     */
    @RequestMapping("importInst")
    @ResponseBody
    public JSONObject importInst( String bk_obj_id,
                                        String bk_supplier_account,
                                       @RequestParam(value = "multipartfiles", required = false) MultipartFile multipartfile, HttpServletRequest request) {
        String bkToken=commParams.getBkTokenByRequest(request);
        String savePath = FileUtils.fileUpload(multipartfile, ConstantKeyFilePath.EXAMPLE_DIR, ConstantKeyFilePath.EXAMPLE_FIX, request);
        if (StringUtils.isEmpty(savePath)) {
            JSONObject result = new JSONObject();
            result.put("result", false);
            result.put("code", 1);
            result.put("message", "未找到指定路径的文件");
            result.put("data", "未找到指定路径的文件");
            return result;
        }

        String fileUploadRootPath = FileUtils.getContextRootPath(request);
        // 获取上传excel绝对路径
        String fullPath = fileUploadRootPath + savePath;
//        String fullPath = "C:\\总结与计划\\host.xlsx";
        //获取当前模块的所有属性值
        if (StringUtils.isBlank(bk_obj_id)) {
            JSONObject result = new JSONObject();
            result.put("result", false);
            result.put("code", 1);
            result.put("message", "获取模块信息失败，请重新操作");
            result.put("data", "获取模块信息失败，请重新操作");
            return result;
        }
        JSONArray bkObjectAttributeJsonarray = this.start(bk_obj_id, bkToken, request);
        if (bkObjectAttributeJsonarray.size() <= 0) {
            JSONObject result = new JSONObject();
            result.put("result", false);
            result.put("code", 1);
            result.put("message", "获取模块信息失败，请重新操作");
            result.put("data", "获取模块信息失败，请重新操作");
            return result;
        }
        //获取所有isonly=true的属性
        List isonlyTrue = new ArrayList();
        for (int d = 0; d < bkObjectAttributeJsonarray.size(); d++) {
            JSONObject isonlyJson = bkObjectAttributeJsonarray.getJSONObject(d);
            if (isonlyJson.getBoolean("isonly")) {
                isonlyTrue.add(isonlyJson.getString("bk_property_id") + "-" + isonlyJson.getString("bk_obj_id"));
            }
        }
        //读取文件
        ImportExcelUtil eu = new ImportExcelUtil(fullPath);
        // 对读取Excel表格内容测试
        JSONArray map = null;
        try {
            map = eu.readExcelContent();
        } catch (Exception e) {
            JSONObject result = new JSONObject();
            result.put("result", false);
            result.put("code", 1);
            result.put("message", "未找到指定路径的文件");
            result.put("data", "未找到指定路径的文件");
            return result;
        }
        if ("host".equals(bk_obj_id)) {
            try {
                if (CollectionUtils.isNotEmpty(map)) {
                    JSONObject resultHostObject=this.hostImport(map, isonlyTrue, bk_obj_id, bkToken, request);
                    return resultHostObject;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            //获取文件
            try {
                if (CollectionUtils.isNotEmpty(isonlyTrue)) {
                    //循环必填属性
                    for (int i = 0; i < isonlyTrue.size(); i++) {
                        String propertyAndObj = isonlyTrue.get(i).toString();
                        //获取当前属性所属的模型，并根据此模型增加数据
                        String bk_property_id = StringUtils.substringBefore(propertyAndObj, "-");
                        String bk_obj_id_new = StringUtils.substringAfter(propertyAndObj, "-");
                        if (StringUtils.isBlank(bk_obj_id_new)) {
                            bk_obj_id_new = bk_obj_id;
                        }
                        //循环EXCEL数据
                        for (int j = 0; j < map.size(); j++) {
                            JSONObject jsonObjectInstObject = map.getJSONObject(j);
                            //若表格数据存在必填属性则继续
                            if (jsonObjectInstObject.keySet().contains(bk_property_id)) {
//                                JSONObject jsonObject = commParams.initJson();
                                JSONObject jsonObject = commParams.initJsonOtherMethod(request);
//                                jsonObject.put("bk_token",bkToken);
                                JSONObject serachInst = new JSONObject();
                                JSONArray bk_weblogicArray = new JSONArray();
                                JSONObject searchOnject = new JSONObject();
                                searchOnject.put("field", bk_property_id);
                                searchOnject.put("operator", "$eq");
                                if (StringUtils.isNotBlank(jsonObjectInstObject.getString(bk_property_id))) {
                                    searchOnject.put("value", jsonObjectInstObject.getString(bk_property_id));
                                } else {
                                    JSONObject result = new JSONObject();
                                    result.put("result", false);
                                    result.put("code", 1);
                                    result.put("message", "实例主键信息必填，请重新操作");
                                    result.put("data", "实例主键信息必填，请重新操作");
                                    return result;
                                }
                                bk_weblogicArray.add(searchOnject);
                                serachInst.put(bk_obj_id_new, bk_weblogicArray);
                                JSONObject pageObject = new JSONObject();
                                pageObject.put("start", 0);
                                pageObject.put("limit", 10000);
                                jsonObject.put("page", pageObject);
                                jsonObject.put("condition", serachInst);
                                jsonObject.put("bk_obj_id", bk_obj_id_new);
                                jsonObject.put("bk_supplier_account", "0");
                                //查询当前实力是否存在，若存在则修改，若不存在则新增
                                JSONObject object = HttpsClient.httpRequest(apiUrlProperties.getSearch_inst_url(), "POST", jsonObject.toString());
                                if (object.getBoolean("result")) {
                                    if (object.getJSONObject("data").getInteger("count") > 0) {
                                        JSONArray bk_inst_idArray = object.getJSONObject("data").getJSONArray("info");
                                        List bk_inst_idList = new ArrayList();
                                        for (int e = 0; e < bk_inst_idArray.size(); e++) {
                                            JSONObject bk_inst_idObject = bk_inst_idArray.getJSONObject(e);
                                            bk_inst_idList.add(bk_inst_idObject.getInteger("bk_inst_id"));
                                        }
//                                        JSONObject updateInstObject = commParams.initJson();
                                        JSONObject updateInstObject = commParams.initJsonOtherMethod(request);
//                                        updateInstObject.put("bk_token",bkToken);
                                        updateInstObject.putAll(map.getJSONObject(j));
                                        updateInstObject.put("bk_obj_id", bk_obj_id_new);
                                        updateInstObject.put("bk_supplier_account", "0");
                                        updateInstObject.put("bk_inst_id", bk_inst_idList.get(0));
                                        JSONObject uppdateInstResultObject = HttpsClient.httpRequest(apiUrlProperties.getUpdate_inst_URL(), "POST", updateInstObject.toString());
                                        if (!uppdateInstResultObject.getBoolean("result")) {
                                            return uppdateInstResultObject;
                                        }
                                    } else {
//                                        JSONObject addInstObject = commParams.initJson();
                                        JSONObject addInstObject = commParams.initJsonOtherMethod(request);
//                                        addInstObject.put("bk_token",bkToken);
                                        addInstObject.putAll(map.getJSONObject(j));
                                        addInstObject.put("bk_obj_id", bk_obj_id);
                                        addInstObject.put("bk_supplier_account", "0");
                                        JSONObject addInstResultObject = HttpsClient.httpRequest(apiUrlProperties.getCreate_inst_URL(), "POST", addInstObject.toString());
                                        if (!addInstResultObject.getBoolean("result")) {
                                            return addInstResultObject;
                                        }
                                    }
                                }
                            }
                        }
                    }
                } else {
                    JSONObject result = new JSONObject();
                    result.put("result", false);
                    result.put("code", 1);
                    result.put("message", "查找实例主键信息失败，请重新操作");
                    result.put("data", "查找实例主键信息失败，请重新操作");
                    return result;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        JSONObject result = new JSONObject();
        result.put("result", true);
        result.put("code", 0);
        result.put("message", "导入成功");
        result.put("data", "导入成功");
        return result;
    }

    //递归取数据
    public void roll(JSONArray jsonArray, JSONObject objectList, List businessList, List setList, List moduleList, List fwList, int num) {

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            if (businessList.get(1).equals(jsonObject.getString("bk_inst_name"))) {
                fwList.add(jsonObject.get("bk_inst_id"));
                rollFw(jsonObject.getJSONArray("child"), businessList, setList, moduleList, fwList, num);
            }
            roll(jsonObject.getJSONArray("child"), objectList, businessList, setList, moduleList, fwList, num);
        }

    }

    public void rollFw(JSONArray jsonArray, List businessList, List setList, List moduleList, List fwList, int num) {

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            //若取道集群，则判断名称是否和业务层级相同，相同则添加进list
            if (businessList.get(2).equals(jsonObject.getString("bk_inst_name"))) {
                if ("set".equals(jsonObject.getString("bk_obj_id"))) {
                    setList.add(jsonObject.get("bk_inst_id"));
                    rollSet(jsonObject.getJSONArray("child"), businessList, setList, moduleList, fwList, num);
                }

            }

            rollFw(jsonObject.getJSONArray("child"), businessList, setList, moduleList, fwList, num);

        }

    }

    public void rollSet(JSONArray jsonArray, List businessList, List setList, List moduleList, List fwList, int num) {

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            //若取道集群，则判断名称是否和业务层级相同，相同则添加进list
            if (businessList.get(3).equals(jsonObject.getString("bk_inst_name"))) {
                if ("module".equals(jsonObject.getString("bk_obj_id"))) {
                    moduleList.add(jsonObject.get("bk_inst_id"));
                }
            }

            rollSet(jsonObject.getJSONArray("child"), businessList, setList, moduleList, fwList, num);

        }

    }

    public JSONArray start(String bk_obj_id, String bkToken, HttpServletRequest request) {
//        JSONObject jsonObject = commParams.initJson();
        JSONObject jsonObject = commParams.initJsonOtherMethod(request);
//        jsonObject.put("bk_token",bkToken);
        JSONArray jsonArray = new JSONArray();
        jsonObject.put("bk_obj_id", bk_obj_id);
        //查询所有的模型属性
        JSONObject search_object_attribute_URLJSON = HttpsClient.httpRequest(apiUrlProperties.getSearch_object_attribute_URL(), "POST", jsonObject.toString());
        if (search_object_attribute_URLJSON != null) {
            JSONArray search_object_attribute_jsonArray = search_object_attribute_URLJSON.getJSONArray("data");
            if (search_object_attribute_jsonArray != null && search_object_attribute_jsonArray.size() > 0) {
                for (int m = 0; m < search_object_attribute_jsonArray.size(); m++) {
                    JSONObject search_object_attribute_obj = search_object_attribute_jsonArray.getJSONObject(m);
                    jsonArray.add(search_object_attribute_obj);
                    String bk_asst_obj_id = search_object_attribute_obj.getString("bk_asst_obj_id");
                    if (StringUtils.isNotBlank(bk_asst_obj_id)) {
                        roll(bk_asst_obj_id, jsonArray, bkToken, request);
                    }
                }
            }
        }
        return jsonArray;
    }

    public JSONArray roll(String bk_obj_id, JSONArray jsonArray, String bkToken, HttpServletRequest request) {
//        JSONObject jsonObject = commParams.initJson();
        JSONObject jsonObject = commParams.initJsonOtherMethod(request);
//        jsonObject.put("bk_token",bkToken);
        jsonObject.put("bk_obj_id", bk_obj_id);
        //查询所有的模型属性
        JSONObject search_object_attribute_URLJSON = HttpsClient.httpRequest(apiUrlProperties.getSearch_object_attribute_URL(), "POST", jsonObject.toString());
        if (search_object_attribute_URLJSON != null) {
            JSONArray search_object_attribute_jsonArray = search_object_attribute_URLJSON.getJSONArray("data");
            for (int m = 0; m < search_object_attribute_jsonArray.size(); m++) {
                JSONObject search_object_attribute_obj = search_object_attribute_jsonArray.getJSONObject(m);
                jsonArray.add(search_object_attribute_obj);
                String bk_asst_obj_id = search_object_attribute_obj.getString("bk_asst_obj_id");
                if (StringUtils.isNotBlank(bk_asst_obj_id)) {
                    roll(bk_asst_obj_id, jsonArray, bkToken, request);
                }
            }
        }
        return jsonArray;
    }


    public JSONObject hostImport(JSONArray map, List isonlyTrue, String bk_obj_id, String bkToken, HttpServletRequest request) {
        if (CollectionUtils.isNotEmpty(isonlyTrue)) {
            //循环必填属性
            for (int i = 0; i < isonlyTrue.size(); i++) {
                String propertyAndObj = isonlyTrue.get(i).toString();
                //获取当前属性所属的模型，并根据此模型增加数据
                String bk_property_id = StringUtils.substringBefore(propertyAndObj, "-");
                String bk_obj_id_new = StringUtils.substringAfter(propertyAndObj, "-");
                if (StringUtils.isBlank(bk_obj_id_new)) {
                    bk_obj_id_new = bk_obj_id;
                }
                //循环EXCEL数据
                for (int j = 0; j < map.size(); j++) {
                    JSONObject jsonObjectInstObject = map.getJSONObject(j);
                    //若表格数据存在必填属性则继续
                    if (jsonObjectInstObject.keySet().contains(bk_property_id)) {
//                        JSONObject jsonObject = commParams.initJson();
                        JSONObject jsonObject = commParams.initJsonOtherMethod(request);
//                        jsonObject.put("bk_token",bkToken);
                        JSONObject serachInst = new JSONObject();
                        JSONArray bk_weblogicArray = new JSONArray();
                        JSONObject searchOnject = new JSONObject();
                        searchOnject.put("field", bk_property_id);
                        searchOnject.put("operator", "$eq");
                        if (StringUtils.isNotBlank(jsonObjectInstObject.getString(bk_property_id))) {
                            searchOnject.put("value", jsonObjectInstObject.getString(bk_property_id));
                        } else {
                            JSONObject result = new JSONObject();
                            result.put("result", false);
                            result.put("code", 1);
                            result.put("message", "实例主键信息必填，请重新操作");
                            result.put("data", "实例主键信息必填，请重新操作");
                            return result;
                        }
                        bk_weblogicArray.add(searchOnject);
                        serachInst.put(bk_obj_id_new, bk_weblogicArray);
                        JSONObject pageObject = new JSONObject();
                        pageObject.put("start", 0);
                        pageObject.put("limit", 10000);
                        jsonObject.put("page", pageObject);
                        jsonObject.put("condition", serachInst);
                        jsonObject.put("bk_obj_id", bk_obj_id_new);
                        jsonObject.put("bk_supplier_account", "0");
                        System.out.println("jsonObject" + jsonObject);
                        //查询当前实例是否存在，若存在则修改，若不存在则新增
//                        JSONObject object = HttpsClient.httpRequest(search_inst_URL, "POST", jsonObject.toString());
                        //查询主机信息是否存在
//                        JSONObject hostJsonSearch = commParams.initJson();
                        JSONObject hostJsonSearch = commParams.initJsonOtherMethod(request);
//                        hostJsonSearch.put("bk_token",bkToken);
                        hostJsonSearch.put("bk_supplier_account", "0");
                        JSONObject ipSearch = new JSONObject();
                        List ipListSearch = new ArrayList();
                        ipListSearch.add(jsonObjectInstObject.getString("bk_host_innerip"));
                        ipSearch.put("data", ipListSearch);
                        ipSearch.put("exact", 1);
                        ipSearch.put("bk_cloud_id", "0");
                        ipSearch.put("flag", "bk_host_innerip|bk_host_outerip");
                        hostJsonSearch.put("ip", ipSearch);
                        JSONObject search_host_Json = HttpsClient.httpRequest(apiUrlProperties.getSearch_host_url(), "POST", hostJsonSearch.toString());

                        if (search_host_Json.getBoolean("result")) {
                            //主机信息存在即修改，否则新增
                            if (search_host_Json.getJSONObject("data").getInteger("count") > 0) {
                                JSONArray bk_inst_idArray = search_host_Json.getJSONObject("data").getJSONArray("info");
                                List bk_inst_idList = new ArrayList();
                                for (int e = 0; e < bk_inst_idArray.size(); e++) {
                                    JSONObject bk_inst_idObject = bk_inst_idArray.getJSONObject(e);
                                    bk_inst_idList.add(bk_inst_idObject.getJSONObject("host").getInteger("bk_host_id"));
                                }
//                                JSONObject updateInstObject = commParams.initJson();
                                JSONObject updateInstObject = commParams.initJsonOtherMethod(request);
//                                updateInstObject.put("bk_token",bkToken);
                                JSONObject excelData = map.getJSONObject(j);
                                String business = excelData.getString("yewucengji");
                                excelData.remove("yewucengji");
//                                updateInstObject.put("bk_obj_id", bk_obj_id_new);
                                updateInstObject.put("bk_supplier_account", "0");
                                updateInstObject.put("data", excelData);
                                updateInstObject.put("bk_host_id", bk_inst_idList.get(0).toString());
//                                JSONObject uppdateInstResultObject = HttpsClient.httpRequest(update_inst_URL, "POST", updateInstObject.toString());
                                JSONObject uppdateInstResultObject = HttpsClient.httpRequest(apiUrlProperties.getUpdate_host_URL(), "POST", updateInstObject.toString());
                                if (!uppdateInstResultObject.getBoolean("result")) {
                                    return uppdateInstResultObject;
                                }
                                if (!uppdateInstResultObject.getBoolean("result")) {
                                    JSONObject result = new JSONObject();
                                    result.put("result", search_host_Json.getBoolean("result"));
                                    result.put("code", search_host_Json.get("code"));
                                    result.put("message", "第" + (i + 1) + "条数据不符合规范，请修改后再次导入！");
                                    result.put("data", search_host_Json.get("data"));
                                    return result;
                                }
                                List<String> businessList = Arrays.asList(business.split("##"));
                                if (StringUtils.isBlank(business) && businessList.size() < 3) {
                                    JSONObject result = new JSONObject();
                                    result.put("result", false);
                                    result.put("code", 1);
                                    result.put("message", "第" + (i + 1) + "条数据不符合业务层级规则，请修改后再次导入！");
                                    result.put("data", "");
                                    return result;
                                }
                                //若不为空，则根据解析后的第一个字段作为业务名称查询业务信息，并获取到业务ID
                                if (CollectionUtils.isNotEmpty(businessList)) {
//                                    JSONObject businessJson = commParams.initJson();
                                    JSONObject businessJson = commParams.initJsonOtherMethod(request);
//                                    businessJson.put("bk_token",bkToken);
                                    JSONArray bk_biz_idArray = new JSONArray();
                                    bk_biz_idArray.add("bk_biz_id");
                                    JSONObject bk_biz_idObject = new JSONObject();
                                    bk_biz_idObject.put("bk_biz_name", businessList.get(0));
                                    businessJson.put("bk_supplier_account", "0");
                                    businessJson.put("fields", bk_biz_idArray);
                                    businessJson.put("condition", bk_biz_idObject);
                                    //根据业务名称获取到业务ID信息
                                    JSONObject search_business_object = HttpsClient.httpRequest(apiUrlProperties.getSearch_business_URL(), "POST", businessJson.toString());
                                    JSONObject businessResultData = search_business_object.getJSONObject("data");
                                    if (!search_business_object.getBoolean("result")) {
                                        JSONObject result = new JSONObject();
                                        result.put("result", search_business_object.getBoolean("result"));
                                        result.put("code", search_business_object.get("code"));
                                        result.put("message", "第" + (i + 1) + "条数据获取业务数据失败，请修改后再次导入！");
                                        result.put("data", search_business_object.get("data"));
                                        return result;
                                    }
                                    if (businessResultData == null || businessResultData.getInteger("count") < 1) {
                                        JSONObject result = new JSONObject();
                                        result.put("result", false);
                                        result.put("code", search_business_object.get("code"));
                                        result.put("message", "第" + (i + 1) + "条数据获取业务数据为空，请修改后再次导入！");
                                        result.put("data", search_business_object.get("data"));
                                        return result;
                                    }
                                    JSONArray businessResultInfo = businessResultData.getJSONArray("info");
                                    if (businessResultInfo.size() < 1) {
                                        JSONObject result = new JSONObject();
                                        result.put("result", false);
                                        result.put("code", search_business_object.get("code"));
                                        result.put("message", "第" + (i + 1) + "条数据获取业务数据为空，请修改后再次导入！");
                                        result.put("data", search_business_object.get("data"));
                                        return result;
                                    }
                                    for (int b = 0; b < businessResultInfo.size(); b++) {
                                        JSONObject bizObject = businessResultInfo.getJSONObject(b);
                                        if (bizObject == null) {
                                            JSONObject result = new JSONObject();
                                            result.put("result", false);
                                            result.put("code", search_business_object.get("code"));
                                            result.put("message", "第" + (i + 1) + "条数据获取业务数据为空，请修改后再次导入！");
                                            result.put("data", search_business_object.get("data"));
                                            return result;
                                        }
                                        Integer bk_biz_id = bizObject.getInteger("bk_biz_id");
//                                        JSONObject searchBizTopo = commParams.initJson();
                                        JSONObject searchBizTopo = commParams.initJsonOtherMethod(request);
//                                        searchBizTopo.put("bk_token",bkToken);
                                        searchBizTopo.put("bk_supplier_account", "0");
                                        searchBizTopo.put("bk_biz_id", bk_biz_id);
                                        //根据不同的业务ID去topo里面查找相对于的拓扑关系
                                        JSONObject search_biz_inst_topo_object = HttpsClient.httpRequest(apiUrlProperties.getSearch_biz_inst_topo_URL(), "POST", searchBizTopo.toString());

                                        if (!search_biz_inst_topo_object.getBoolean("result")) {
                                            JSONObject result = new JSONObject();
                                            result.put("result", false);
                                            result.put("code", search_biz_inst_topo_object.get("code"));
                                            result.put("message", "第" + (i + 1) + "条数据获取业务实例拓扑失败，请修改后再次导入！");
                                            result.put("data", search_biz_inst_topo_object.get("data"));
                                            return result;
                                        }
                                        JSONArray topuArray = search_biz_inst_topo_object.getJSONArray("data");
                                        if (topuArray.size() < 0) {
                                            JSONObject result = new JSONObject();
                                            result.put("result", false);
                                            result.put("code", search_biz_inst_topo_object.get("code"));
                                            result.put("message", "第" + (i + 1) + "条数据无业务实例拓扑，请修改后再次导入！");
                                            result.put("data", search_biz_inst_topo_object.get("data"));
                                            return result;
                                        }
                                        for (int k = 0; k < topuArray.size(); k++) {
                                            JSONObject topoObject = topuArray.getJSONObject(k);
                                            //若一级目录为业务则继续取其下级信息
                                            if ("biz".equals(topoObject.getString("bk_obj_id"))) {
                                                if (businessList.get(0).equals(topoObject.getString("bk_inst_name"))) {
                                                    JSONArray bizChild = topoObject.getJSONArray("child");
                                                    if (bizChild.size() < 1) {
                                                        JSONObject result = new JSONObject();
                                                        result.put("result", false);
                                                        result.put("code", search_biz_inst_topo_object.get("code"));
                                                        result.put("message", "第" + (i + 1) + "条数据无集群拓扑，请修改后再次导入！");
                                                        result.put("data", search_biz_inst_topo_object.get("data"));
                                                        return result;
                                                    }
                                                    //第一步添加主机至资源池
//                                                    JSONObject add_host_to_resourceObject = commParams.initJson();
                                                    JSONObject add_host_to_resourceObject = commParams.initJsonOtherMethod(request);
//                                                    add_host_to_resourceObject.put("bk_token",bkToken);
                                                    add_host_to_resourceObject.put("bk_supplier_account", "0");
                                                    add_host_to_resourceObject.put("bk_biz_id", bk_biz_id);
                                                    JSONObject host_info_index = new JSONObject();
                                                    host_info_index.put("import_from", "3");
                                                    host_info_index.put("bk_cloud_id", 0);
                                                    host_info_index.put("bk_host_innerip", updateInstObject.getString("bk_host_innerip"));
                                                    JSONObject host_info = new JSONObject();
                                                    host_info.put("0", host_info_index);
                                                    add_host_to_resourceObject.put("host_info", host_info);
                                                    System.out.println("add_host_to_resourceObject" + add_host_to_resourceObject);
                                                    JSONObject add_host_to_resource_object = HttpsClient.httpRequest(apiUrlProperties.getAdd_host_to_resource_URL(), "POST", add_host_to_resourceObject.toString());
                                                    if (!add_host_to_resource_object.getBoolean("result")) {
                                                        return add_host_to_resource_object;
                                                    }

                                                    //第二步查询主机信息
//                                                    JSONObject hostJson = commParams.initJson();
                                                    JSONObject hostJson = commParams.initJsonOtherMethod(request);
//                                                    hostJson.put("bk_token",bkToken);
                                                    hostJson.put("bk_supplier_account", "0");
                                                    JSONObject ip = new JSONObject();
                                                    List ipList = new ArrayList();
                                                    ipList.add(updateInstObject.getString("bk_host_innerip"));
                                                    ip.put("data", ipList);
                                                    ip.put("exact", 1);
                                                    ip.put("bk_cloud_id", "0");
                                                    ip.put("flag", "bk_host_innerip|bk_host_outerip");
                                                    hostJson.put("ip", ip);
                                                    JSONObject search_host_object = HttpsClient.httpRequest(apiUrlProperties.getSearch_host_url(), "POST", hostJson.toString());
                                                    if (!search_host_object.getBoolean("result")) {
                                                        return search_host_object;
                                                    }
                                                    System.out.println("----" + hostJson);

                                                    JSONObject hostObject = search_host_object.getJSONObject("data");
                                                    System.out.println("****" + hostObject);
                                                    if (hostObject == null) {
                                                        JSONObject result = new JSONObject();
                                                        result.put("result", false);
                                                        result.put("code", search_biz_inst_topo_object.get("code"));
                                                        result.put("message", "第" + (i + 1) + "条数据无主机信息，请修改后再次导入！");
                                                        result.put("data", search_biz_inst_topo_object.get("data"));
                                                        return result;
                                                    }
                                                    JSONArray infoArray = hostObject.getJSONArray("info");
                                                    if (infoArray.size() <= 0) {
                                                        JSONObject result = new JSONObject();
                                                        result.put("result", false);
                                                        result.put("code", search_biz_inst_topo_object.get("code"));
                                                        result.put("message", "第" + (i + 1) + "条数据无主机信息，请修改后再次导入！");
                                                        result.put("data", search_biz_inst_topo_object.get("data"));
                                                        return result;
                                                    }
                                                    List bk_host_id_list = new ArrayList();
                                                    for (int a = 0; a < infoArray.size(); a++) {
                                                        JSONObject hostObjectData = infoArray.getJSONObject(a).getJSONObject("host");
                                                        Integer bk_host_id = hostObjectData.getInteger("bk_host_id");
                                                        bk_host_id_list.add(bk_host_id);
                                                    }

                                                    //递归
                                                    List setList = new ArrayList();
                                                    List moduleList = new ArrayList();
                                                    List fwList = new ArrayList();
                                                    JSONObject rollJson = new JSONObject();
                                                    roll(bizChild, rollJson, businessList, setList, moduleList, fwList, i);

                                                    //查询集群是否存在，不存在则新增
//                                                    JSONObject setObject = commParams.initJson();
                                                    JSONObject setObject = commParams.initJsonOtherMethod(request);
//                                                    setObject.put("bk_token",bkToken);
                                                    setObject.put("bk_supplier_account", "0");
                                                    setObject.put("bk_biz_id", bk_biz_id);
                                                    JSONArray setArrayId = new JSONArray();
                                                    setArrayId.add("bk_set_id");
                                                    setObject.put("fields", setArrayId);
                                                    JSONObject setObjectearch = new JSONObject();
                                                    setObjectearch.put("bk_set_name", businessList.get(2));
                                                    setObject.put("condition", setObjectearch);
                                                    JSONObject pages = new JSONObject();
                                                    pages.put("start", 0);
                                                    pages.put("limit", 100);
                                                    setObject.put("page", pages);
                                                    System.out.println("setObject" + setObject);
                                                    JSONObject search_set_object = HttpsClient.httpRequest(apiUrlProperties.getSearch_set_URL(), "POST", setObject.toString());
                                                    Integer search_bk_set_id = 0;
                                                    if (search_set_object.getBoolean("result")) {
                                                        //未查找到集群，新增集群
                                                        if (search_set_object.getJSONObject("data").getInteger("count") <= 0) {
//                                                            JSONObject setAddObject = commParams.initJson();
                                                            JSONObject setAddObject = commParams.initJsonOtherMethod(request);
//                                                            setAddObject.put("bk_token",bkToken);
                                                            setAddObject.put("bk_supplier_account", "0");
                                                            setAddObject.put("bk_biz_id", bk_biz_id);
                                                            JSONObject dataSet = new JSONObject();
                                                            dataSet.put("bk_parent_id", fwList.get(0));
                                                            dataSet.put("bk_set_name", businessList.get(2));
                                                            setAddObject.put("data", dataSet);
                                                            JSONObject create_set_object = HttpsClient.httpRequest(apiUrlProperties.getCreate_set_URL(), "POST", setAddObject.toString());
                                                            if (create_set_object.getBoolean("result")) {
                                                                search_bk_set_id = create_set_object.getJSONObject("data").getInteger("bk_set_id");
                                                            } else {
                                                                return create_set_object;
                                                            }
                                                        } else {
                                                            JSONArray setArray = search_set_object.getJSONObject("data").getJSONArray("info");
                                                            for (int c = 0; c < setArray.size(); c++) {
                                                                JSONObject setIndex = setArray.getJSONObject(c);
                                                                search_bk_set_id = setIndex.getInteger("bk_set_id");
                                                            }
                                                        }
                                                    } else {
                                                        return search_set_object;
                                                    }

                                                    //查询模块是否存在，不存在则新增
//                                                    JSONObject moduleObject = commParams.initJson();
                                                    JSONObject moduleObject = commParams.initJsonOtherMethod(request);
//                                                    moduleObject.put("bk_token",bkToken);
                                                    moduleObject.put("bk_supplier_account", "0");
                                                    moduleObject.put("bk_biz_id", bk_biz_id);
                                                    moduleObject.put("bk_set_id", search_bk_set_id);
                                                    JSONArray moduleArrayId = new JSONArray();
                                                    moduleArrayId.add("bk_module_id");
                                                    moduleObject.put("fields", moduleArrayId);
                                                    JSONObject moduleObjectearch = new JSONObject();
                                                    moduleObjectearch.put("bk_module_name", businessList.get(3));
                                                    moduleObject.put("condition", moduleObjectearch);
                                                    JSONObject search_module_object = HttpsClient.httpRequest(apiUrlProperties.getSearch_module_URL(), "POST", moduleObject.toString());
                                                    Integer search_bk_module_id = 0;
                                                    if (search_set_object.getBoolean("result")) {
                                                        //未查找到模块，新增模块
                                                        if (search_module_object.getJSONObject("data").getInteger("count") <= 0) {
//                                                            JSONObject moduleAddObject = commParams.initJson();
                                                            JSONObject moduleAddObject = commParams.initJsonOtherMethod(request);
//                                                            moduleAddObject.put("bk_token",bkToken);
                                                            moduleAddObject.put("bk_supplier_account", "0");
                                                            moduleAddObject.put("bk_biz_id", bk_biz_id);
                                                            moduleAddObject.put("bk_set_id", search_bk_set_id);
                                                            JSONObject dataSet = new JSONObject();
                                                            dataSet.put("bk_parent_id", search_bk_set_id);
                                                            dataSet.put("bk_module_name", businessList.get(3));
                                                            moduleAddObject.put("data", dataSet);
                                                            JSONObject create_module_object = HttpsClient.httpRequest(apiUrlProperties.getCreate_module_URL(), "POST", moduleAddObject.toString());
                                                            if (create_module_object.getBoolean("result")) {
                                                                search_bk_module_id = create_module_object.getJSONObject("data").getInteger("bk_set_id");
                                                            } else {
                                                                return create_module_object;
                                                            }
                                                        } else {
                                                            JSONArray setArray = search_set_object.getJSONObject("data").getJSONArray("info");
                                                            for (int c = 0; c < setArray.size(); c++) {
                                                                JSONObject setIndex = setArray.getJSONObject(c);
                                                                search_bk_module_id = setIndex.getInteger("bk_set_id");
                                                            }
                                                        }
                                                    } else {
                                                        return search_set_object;
                                                    }

                                                    //根据不同的业务ID去topo里面查找相对于的拓扑关系
                                                    JSONObject search_biz_inst_topo_object_new = HttpsClient.httpRequest(apiUrlProperties.getSearch_biz_inst_topo_URL(), "POST", searchBizTopo.toString());
                                                    System.out.println("search_biz_inst_topo_object_new----" + search_biz_inst_topo_object_new + "------");
                                                    if (!search_biz_inst_topo_object_new.getBoolean("result")) {
                                                        JSONObject result = new JSONObject();
                                                        result.put("result", false);
                                                        result.put("code", search_biz_inst_topo_object_new.get("code"));
                                                        result.put("message", "第" + (i + 1) + "条数据获取业务实例拓扑失败，请修改后再次导入！");
                                                        result.put("data", search_biz_inst_topo_object_new.get("data"));
                                                        return result;
                                                    }
                                                    JSONArray topuArrayNew = search_biz_inst_topo_object_new.getJSONArray("data");
                                                    if (topuArray.size() < 0) {
                                                        JSONObject result = new JSONObject();
                                                        result.put("result", false);
                                                        result.put("code", search_biz_inst_topo_object_new.get("code"));
                                                        result.put("message", "第" + (i + 1) + "条数据无业务实例拓扑，请修改后再次导入！");
                                                        result.put("data", search_biz_inst_topo_object_new.get("data"));
                                                        return result;
                                                    }
                                                    List setListNew = new ArrayList();
                                                    List moduleListNew = new ArrayList();
                                                    List fwListNew = new ArrayList();
                                                    JSONObject rollJsonNew = new JSONObject();
                                                    for (int f = 0; f < topuArrayNew.size(); f++) {
                                                        JSONObject topoObjectNew = topuArrayNew.getJSONObject(k);
                                                        //若一级目录为业务则继续取其下级信息
                                                        if ("biz".equals(topoObjectNew.getString("bk_obj_id"))) {
                                                            if (businessList.get(0).equals(topoObjectNew.getString("bk_inst_name"))) {
                                                                JSONArray bizChildNew = topoObjectNew.getJSONArray("child");
                                                                roll(bizChildNew, rollJsonNew, businessList, setListNew, moduleListNew, fwListNew, i);
                                                            }
                                                        }
                                                    }
//                                                    JSONObject transferObject = commParams.initJson();
                                                    JSONObject transferObject = commParams.initJsonOtherMethod(request);
//                                                    transferObject.put("bk_token",bkToken);
                                                    transferObject.put("bk_supplier_account", "0");
                                                    transferObject.put("bk_biz_id", bk_biz_id);
                                                    transferObject.put("bk_host_id", bk_host_id_list);
                                                    if (moduleListNew.size() < 0) {
                                                        JSONObject result = new JSONObject();
                                                        result.put("result", false);
                                                        result.put("code", 1);
                                                        result.put("message", "第" + (i + 1) + "条数据未查找到模块信息，请修改后再次导入！");
                                                        result.put("data", "");
                                                        return result;
                                                    }
                                                    transferObject.put("bk_module_id", moduleListNew);
                                                    transferObject.put("is_increment", true);

//                                                    JSONObject transfer_resourcehost_to_idlemoduleObject = commParams.initJson();
                                                    JSONObject transfer_resourcehost_to_idlemoduleObject = commParams.initJsonOtherMethod(request);
//                                                    transfer_resourcehost_to_idlemoduleObject.put("bk_token",bkToken);
                                                    transfer_resourcehost_to_idlemoduleObject.put("bk_supplier_account", "0");
                                                    transfer_resourcehost_to_idlemoduleObject.put("bk_biz_id", bk_biz_id);
                                                    transfer_resourcehost_to_idlemoduleObject.put("bk_host_id", bk_host_id_list);
                                                    //第三步资源池主机分配至业务的空闲机模块
                                                    JSONObject transfer_resourcehost_to_idlemodule_object = HttpsClient.httpRequest(apiUrlProperties.getTransfer_resourcehost_to_idlemodule_URL(), "POST", transfer_resourcehost_to_idlemoduleObject.toString());
//                                            if (!transfer_resourcehost_to_idlemodule_object.getBoolean("result")){
//                                                return transfer_resourcehost_to_idlemodule_object;
//                                            }

//                                            System.out.println("transferObject"+transferObject);

                                                    //第四步根据渠道的业务ID，集群ID，模块ID进行业务内主机转移
                                                    JSONObject transfer_host_module_object = HttpsClient.httpRequest(apiUrlProperties.getTransfer_host_module_URL(), "POST", transferObject.toString());
                                                    if (!transfer_host_module_object.getBoolean("result")) {
                                                        return transfer_host_module_object;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    JSONObject result = new JSONObject();
                                    result.put("result", false);
                                    result.put("code", 1);
                                    result.put("message", "第" + (i + 1) + "条数据不符合规范，请修改后再次导入！");
                                    result.put("data", "");
                                    return result;
                                }
                            } else {
                                //新增主机内网IP至实例信息中
                                JSONObject updateInstObject = map.getJSONObject(j);
//                                JSONObject create_inst_json=new JSONObject();
                                String business = updateInstObject.getString("yewucengji");
                                updateInstObject.remove("yewucengji");
//                                create_inst_json.put("bk_app_code", bk_app_code);
//                                create_inst_json.put("bk_app_secret", bk_app_secret);
//                                create_inst_json.put("bk_token", bk_token);
//                                create_inst_json.put("bk_obj_id", bk_obj_id_new);
//                                create_inst_json.put("bk_supplier_account", "0");
//                                create_inst_json.putAll(updateInstObject);
//                                JSONObject addInstResultObject = HttpsClient.httpRequest(create_inst_URL, "POST", create_inst_json.toString());
//                                if (!addInstResultObject.getBoolean("result")) {
//                                    JSONObject result = new JSONObject();
//                                    result.put("result", search_host_Json.getBoolean("result"));
//                                    result.put("code", search_host_Json.get("code"));
//                                    result.put("message", "第" + (i + 1) + "条数据不符合规范，请修改后再次导入！");
//                                    result.put("data", search_host_Json.get("data"));
//                                    return result;
//                                }
                                List<String> businessList = Arrays.asList(business.split("##"));
//                                if (StringUtils.isBlank(business) && businessList.size() < 3) {
//                                    JSONObject result = new JSONObject();
//                                    result.put("result", false);
//                                    result.put("code", 1);
//                                    result.put("message", "第" + (i + 1) + "条数据不符合业务层级规则，请修改后再次导入！");
//                                    result.put("data", "");
//                                    return result;
//                                }
//                                Long bk_host_id=addInstResultObject.getJSONObject("data").getLong("bk_host_id");
//                                //根据内网ip修改当前主机信息
//                                JSONObject updatehostObject = new JSONObject();
//                                JSONObject excelDataNew=map.getJSONObject(j);
//                                excelDataNew.remove("yewucengji");
//                                updatehostObject.put("bk_app_code", bk_app_code);
//                                updatehostObject.put("bk_app_secret", bk_app_secret);
//                                updatehostObject.put("bk_token", bk_token);
////                                updateInstObject.put("bk_obj_id", bk_obj_id_new);
//                                updatehostObject.put("bk_supplier_account", "0");
//                                updatehostObject.put("data", excelDataNew);
//                                updatehostObject.put("bk_host_id", bk_host_id.toString());
//                                JSONObject uppdateInstResultObject = HttpsClient.httpRequest(update_host_URL, "POST", updatehostObject.toString());
//                                if (!uppdateInstResultObject.getBoolean("result")) {
//                                    return uppdateInstResultObject;
//                                }
                                //若不为空，则根据解析后的第一个字段作为业务名称查询业务信息，并获取到业务ID
                                if (CollectionUtils.isNotEmpty(businessList)) {
//                                    JSONObject businessJson = commParams.initJson();
                                    JSONObject businessJson = commParams.initJsonOtherMethod(request);
//                                    businessJson.put("bk_token",bkToken);
                                    JSONArray bk_biz_idArray = new JSONArray();
                                    bk_biz_idArray.add("bk_biz_id");
                                    JSONObject bk_biz_idObject = new JSONObject();
                                    bk_biz_idObject.put("bk_biz_name", businessList.get(0));
                                    businessJson.put("bk_supplier_account", "0");
                                    businessJson.put("fields", bk_biz_idArray);
                                    businessJson.put("condition", bk_biz_idObject);
                                    //根据业务名称获取到业务ID信息
                                    JSONObject search_business_object = HttpsClient.httpRequest(apiUrlProperties.getSearch_business_URL(), "POST", businessJson.toString());
                                    JSONObject businessResultData = search_business_object.getJSONObject("data");
                                    if (!search_business_object.getBoolean("result")) {
                                        JSONObject result = new JSONObject();
                                        result.put("result", search_business_object.getBoolean("result"));
                                        result.put("code", search_business_object.get("code"));
                                        result.put("message", "第" + (i + 1) + "条数据获取业务数据失败，请修改后再次导入！");
                                        result.put("data", search_business_object.get("data"));
                                        return result;
                                    }
                                    if (businessResultData == null || businessResultData.getInteger("count") < 1) {
                                        JSONObject result = new JSONObject();
                                        result.put("result", false);
                                        result.put("code", search_business_object.get("code"));
                                        result.put("message", "第" + (i + 1) + "条数据获取业务数据为空，请修改后再次导入！");
                                        result.put("data", search_business_object.get("data"));
                                        return result;
                                    }
                                    JSONArray businessResultInfo = businessResultData.getJSONArray("info");
                                    if (businessResultInfo.size() < 1) {
                                        JSONObject result = new JSONObject();
                                        result.put("result", false);
                                        result.put("code", search_business_object.get("code"));
                                        result.put("message", "第" + (i + 1) + "条数据获取业务数据为空，请修改后再次导入！");
                                        result.put("data", search_business_object.get("data"));
                                        return result;
                                    }
                                    for (int b = 0; b < businessResultInfo.size(); b++) {
                                        JSONObject bizObject = businessResultInfo.getJSONObject(b);
                                        if (bizObject == null) {
                                            JSONObject result = new JSONObject();
                                            result.put("result", false);
                                            result.put("code", search_business_object.get("code"));
                                            result.put("message", "第" + (i + 1) + "条数据获取业务数据为空，请修改后再次导入！");
                                            result.put("data", search_business_object.get("data"));
                                            return result;
                                        }
                                        Integer bk_biz_id = bizObject.getInteger("bk_biz_id");
//                                        JSONObject searchBizTopo = commParams.initJson();
                                        JSONObject searchBizTopo = commParams.initJsonOtherMethod(request);
//                                        searchBizTopo.put("bk_token",bkToken);
                                        searchBizTopo.put("bk_supplier_account", "0");
                                        searchBizTopo.put("bk_biz_id", bk_biz_id);
                                        //根据不同的业务ID去topo里面查找相对于的拓扑关系
                                        JSONObject search_biz_inst_topo_object = HttpsClient.httpRequest(apiUrlProperties.getSearch_biz_inst_topo_URL(), "POST", searchBizTopo.toString());

                                        if (!search_biz_inst_topo_object.getBoolean("result")) {
                                            JSONObject result = new JSONObject();
                                            result.put("result", false);
                                            result.put("code", search_biz_inst_topo_object.get("code"));
                                            result.put("message", "第" + (i + 1) + "条数据获取业务实例拓扑失败，请修改后再次导入！");
                                            result.put("data", search_biz_inst_topo_object.get("data"));
                                            return result;
                                        }
                                        JSONArray topuArray = search_biz_inst_topo_object.getJSONArray("data");
                                        if (topuArray.size() < 0) {
                                            JSONObject result = new JSONObject();
                                            result.put("result", false);
                                            result.put("code", search_biz_inst_topo_object.get("code"));
                                            result.put("message", "第" + (i + 1) + "条数据无业务实例拓扑，请修改后再次导入！");
                                            result.put("data", search_biz_inst_topo_object.get("data"));
                                            return result;
                                        }
                                        for (int k = 0; k < topuArray.size(); k++) {
                                            JSONObject topoObject = topuArray.getJSONObject(k);
                                            //若一级目录为业务则继续取其下级信息
                                            if ("biz".equals(topoObject.getString("bk_obj_id"))) {
                                                if (businessList.get(0).equals(topoObject.getString("bk_inst_name"))) {
                                                    JSONArray bizChild = topoObject.getJSONArray("child");
                                                    if (bizChild.size() < 1) {
                                                        JSONObject result = new JSONObject();
                                                        result.put("result", false);
                                                        result.put("code", search_biz_inst_topo_object.get("code"));
                                                        result.put("message", "第" + (i + 1) + "条数据无集群拓扑，请修改后再次导入！");
                                                        result.put("data", search_biz_inst_topo_object.get("data"));
                                                        return result;
                                                    }
                                                    //第一步添加主机至资源池
//                                                    JSONObject add_host_to_resourceObject = commParams.initJson();
                                                    JSONObject add_host_to_resourceObject = commParams.initJsonOtherMethod(request);
//                                                    add_host_to_resourceObject.put("bk_token",bkToken);
                                                    add_host_to_resourceObject.put("bk_supplier_account", "0");
                                                    add_host_to_resourceObject.put("bk_biz_id", bk_biz_id);
                                                    JSONObject host_info_index = new JSONObject();
                                                    host_info_index.put("import_from", "3");
                                                    host_info_index.put("bk_cloud_id", "0");
                                                    host_info_index.putAll(updateInstObject);
//                                                    host_info_index.put("bk_host_innerip", updateInstObject.getString("bk_host_innerip"));
                                                    JSONObject host_info = new JSONObject();
                                                    host_info.put("0", host_info_index);
                                                    add_host_to_resourceObject.put("host_info", host_info);
                                                    System.out.println("add_host_to_resourceObject" + add_host_to_resourceObject);
                                                    JSONObject add_host_to_resource_object = HttpsClient.httpRequest(apiUrlProperties.getAdd_host_to_resource_URL(), "POST", add_host_to_resourceObject.toString());
                                                    if (!add_host_to_resource_object.getBoolean("result")) {
                                                        return add_host_to_resource_object;
                                                    }

                                                    //第二步查询主机信息
//                                                    JSONObject hostJson = commParams.initJson();
                                                    JSONObject hostJson = commParams.initJsonOtherMethod(request);
//                                                    hostJson.put("bk_token",bkToken);
                                                    hostJson.put("bk_supplier_account", "0");
                                                    JSONObject ip = new JSONObject();
                                                    List ipList = new ArrayList();
                                                    ipList.add(updateInstObject.getString("bk_host_innerip"));
                                                    ip.put("data", ipList);
                                                    ip.put("exact", 1);
                                                    ip.put("bk_cloud_id", "0");
                                                    ip.put("flag", "bk_host_innerip|bk_host_outerip");
                                                    hostJson.put("ip", ip);
                                                    JSONObject search_host_object = HttpsClient.httpRequest(apiUrlProperties.getSearch_host_url(), "POST", hostJson.toString());
                                                    if (!search_host_object.getBoolean("result")) {
                                                        return search_host_object;
                                                    }
                                                    System.out.println("----" + hostJson);

                                                    JSONObject hostObject = search_host_object.getJSONObject("data");
                                                    System.out.println("****" + hostObject);
                                                    if (hostObject == null) {
                                                        JSONObject result = new JSONObject();
                                                        result.put("result", false);
                                                        result.put("code", search_biz_inst_topo_object.get("code"));
                                                        result.put("message", "第" + (i + 1) + "条数据无主机信息，请修改后再次导入！");
                                                        result.put("data", search_biz_inst_topo_object.get("data"));
                                                        return result;
                                                    }
                                                    JSONArray infoArray = hostObject.getJSONArray("info");
                                                    if (infoArray.size() <= 0) {
                                                        JSONObject result = new JSONObject();
                                                        result.put("result", false);
                                                        result.put("code", search_biz_inst_topo_object.get("code"));
                                                        result.put("message", "第" + (i + 1) + "条数据无主机信息，请修改后再次导入！");
                                                        result.put("data", search_biz_inst_topo_object.get("data"));
                                                        return result;
                                                    }
                                                    List bk_host_id_list = new ArrayList();
                                                    for (int a = 0; a < infoArray.size(); a++) {
                                                        JSONObject hostObjectData = infoArray.getJSONObject(a).getJSONObject("host");
                                                        Integer bk_host_idLong = hostObjectData.getInteger("bk_host_id");
                                                        bk_host_id_list.add(bk_host_idLong);
                                                    }

                                                    //递归
                                                    List setList = new ArrayList();
                                                    List moduleList = new ArrayList();
                                                    List fwList = new ArrayList();
                                                    JSONObject rollJson = new JSONObject();
                                                    roll(bizChild, rollJson, businessList, setList, moduleList, fwList, i);

                                                    //查询集群是否存在，不存在则新增
//                                                    JSONObject setObject = commParams.initJson();
                                                    JSONObject setObject = commParams.initJsonOtherMethod(request);
//                                                    setObject.put("bk_token",bkToken);
                                                    setObject.put("bk_supplier_account", "0");
                                                    setObject.put("bk_biz_id", bk_biz_id);
                                                    JSONArray setArrayId = new JSONArray();
                                                    setArrayId.add("bk_set_id");
                                                    setObject.put("fields", setArrayId);
                                                    JSONObject setObjectearch = new JSONObject();
                                                    setObjectearch.put("bk_set_name", businessList.get(2));
                                                    setObject.put("condition", setObjectearch);
                                                    JSONObject pages = new JSONObject();
                                                    pages.put("start", 0);
                                                    pages.put("limit", 100);
                                                    setObject.put("page", pages);
                                                    System.out.println("setObject" + setObject);
                                                    JSONObject search_set_object = HttpsClient.httpRequest(apiUrlProperties.getSearch_set_URL(), "POST", setObject.toString());
                                                    Integer search_bk_set_id = 0;
                                                    if (search_set_object.getBoolean("result")) {
                                                        //未查找到集群，新增集群
                                                        if (search_set_object.getJSONObject("data").getInteger("count") <= 0) {
//                                                            JSONObject setAddObject = commParams.initJson();
                                                            JSONObject setAddObject = commParams.initJsonOtherMethod(request);
//                                                            setAddObject.put("bk_token",bkToken);
                                                            setAddObject.put("bk_supplier_account", "0");
                                                            setAddObject.put("bk_biz_id", bk_biz_id);
                                                            JSONObject dataSet = new JSONObject();
                                                            dataSet.put("bk_parent_id", fwList.get(0));
                                                            dataSet.put("bk_set_name", businessList.get(2));
                                                            setAddObject.put("data", dataSet);
                                                            JSONObject create_set_object = HttpsClient.httpRequest(apiUrlProperties.getCreate_set_URL(), "POST", setAddObject.toString());
                                                            if (create_set_object.getBoolean("result")) {
                                                                search_bk_set_id = create_set_object.getJSONObject("data").getInteger("bk_set_id");
                                                            } else {
                                                                return create_set_object;
                                                            }
                                                        } else {
                                                            JSONArray setArray = search_set_object.getJSONObject("data").getJSONArray("info");
                                                            for (int c = 0; c < setArray.size(); c++) {
                                                                JSONObject setIndex = setArray.getJSONObject(c);
                                                                search_bk_set_id = setIndex.getInteger("bk_set_id");
                                                            }
                                                        }
                                                    } else {
                                                        return search_set_object;
                                                    }

                                                    //查询模块是否存在，不存在则新增
//                                                    JSONObject moduleObject = commParams.initJson();
                                                    JSONObject moduleObject = commParams.initJsonOtherMethod(request);
//                                                    moduleObject.put("bk_token",bkToken);
                                                    moduleObject.put("bk_supplier_account", "0");
                                                    moduleObject.put("bk_biz_id", bk_biz_id);
                                                    moduleObject.put("bk_set_id", search_bk_set_id);
                                                    JSONArray moduleArrayId = new JSONArray();
                                                    moduleArrayId.add("bk_module_id");
                                                    moduleObject.put("fields", moduleArrayId);
                                                    JSONObject moduleObjectearch = new JSONObject();
                                                    moduleObjectearch.put("bk_module_name", businessList.get(3));
                                                    moduleObject.put("condition", moduleObjectearch);
                                                    JSONObject search_module_object = HttpsClient.httpRequest(apiUrlProperties.getSearch_module_URL(), "POST", moduleObject.toString());
                                                    Integer search_bk_module_id = 0;
                                                    if (search_set_object.getBoolean("result")) {
                                                        //未查找到模块，新增模块
                                                        if (search_module_object.getJSONObject("data").getInteger("count") <= 0) {
//                                                            JSONObject moduleAddObject = commParams.initJson();
                                                            JSONObject moduleAddObject = commParams.initJsonOtherMethod(request);
//                                                            moduleAddObject.put("bk_token",bkToken);
                                                            moduleAddObject.put("bk_supplier_account", "0");
                                                            moduleAddObject.put("bk_biz_id", bk_biz_id);
                                                            moduleAddObject.put("bk_set_id", search_bk_set_id);
                                                            JSONObject dataSet = new JSONObject();
                                                            dataSet.put("bk_parent_id", search_bk_set_id);
                                                            dataSet.put("bk_module_name", businessList.get(3));
                                                            moduleAddObject.put("data", dataSet);
                                                            JSONObject create_module_object = HttpsClient.httpRequest(apiUrlProperties.getCreate_module_URL(), "POST", moduleAddObject.toString());
                                                            if (create_module_object.getBoolean("result")) {
                                                                search_bk_module_id = create_module_object.getJSONObject("data").getInteger("bk_set_id");
                                                            } else {
                                                                return create_module_object;
                                                            }
                                                        } else {
                                                            JSONArray setArray = search_set_object.getJSONObject("data").getJSONArray("info");
                                                            for (int c = 0; c < setArray.size(); c++) {
                                                                JSONObject setIndex = setArray.getJSONObject(c);
                                                                search_bk_module_id = setIndex.getInteger("bk_set_id");
                                                            }
                                                        }
                                                    } else {
                                                        return search_set_object;
                                                    }

                                                    //根据不同的业务ID去topo里面查找相对于的拓扑关系
                                                    JSONObject search_biz_inst_topo_object_new = HttpsClient.httpRequest(apiUrlProperties.getSearch_biz_inst_topo_URL(), "POST", searchBizTopo.toString());
                                                    System.out.println("search_biz_inst_topo_object_new----" + search_biz_inst_topo_object_new + "------");
                                                    if (!search_biz_inst_topo_object_new.getBoolean("result")) {
                                                        JSONObject result = new JSONObject();
                                                        result.put("result", false);
                                                        result.put("code", search_biz_inst_topo_object_new.get("code"));
                                                        result.put("message", "第" + (i + 1) + "条数据获取业务实例拓扑失败，请修改后再次导入！");
                                                        result.put("data", search_biz_inst_topo_object_new.get("data"));
                                                        return result;
                                                    }
                                                    JSONArray topuArrayNew = search_biz_inst_topo_object_new.getJSONArray("data");
                                                    if (topuArray.size() < 0) {
                                                        JSONObject result = new JSONObject();
                                                        result.put("result", false);
                                                        result.put("code", search_biz_inst_topo_object_new.get("code"));
                                                        result.put("message", "第" + (i + 1) + "条数据无业务实例拓扑，请修改后再次导入！");
                                                        result.put("data", search_biz_inst_topo_object_new.get("data"));
                                                        return result;
                                                    }
                                                    List setListNew = new ArrayList();
                                                    List moduleListNew = new ArrayList();
                                                    List fwListNew = new ArrayList();
                                                    JSONObject rollJsonNew = new JSONObject();
                                                    for (int f = 0; f < topuArrayNew.size(); f++) {
                                                        JSONObject topoObjectNew = topuArrayNew.getJSONObject(k);
                                                        //若一级目录为业务则继续取其下级信息
                                                        if ("biz".equals(topoObjectNew.getString("bk_obj_id"))) {
                                                            if (businessList.get(0).equals(topoObjectNew.getString("bk_inst_name"))) {
                                                                JSONArray bizChildNew = topoObjectNew.getJSONArray("child");
                                                                roll(bizChildNew, rollJsonNew, businessList, setListNew, moduleListNew, fwListNew, i);
                                                            }
                                                        }
                                                    }
//                                                    JSONObject transferObject = commParams.initJson();
                                                    JSONObject transferObject = commParams.initJsonOtherMethod(request);
//                                                    transferObject.put("bk_token",bkToken);
                                                    transferObject.put("bk_supplier_account", "0");
                                                    transferObject.put("bk_biz_id", bk_biz_id);
                                                    transferObject.put("bk_host_id", bk_host_id_list);
                                                    if (moduleListNew.size() < 0) {
                                                        JSONObject result = new JSONObject();
                                                        result.put("result", false);
                                                        result.put("code", 1);
                                                        result.put("message", "第" + (i + 1) + "条数据未查找到模块信息，请修改后再次导入！");
                                                        result.put("data", "");
                                                        return result;
                                                    }
                                                    transferObject.put("bk_module_id", moduleListNew);
                                                    transferObject.put("is_increment", true);

//                                                    JSONObject transfer_resourcehost_to_idlemoduleObject = commParams.initJson();
                                                    JSONObject transfer_resourcehost_to_idlemoduleObject = commParams.initJsonOtherMethod(request);
//                                                    transfer_resourcehost_to_idlemoduleObject.put("bk_token",bkToken);
                                                    transfer_resourcehost_to_idlemoduleObject.put("bk_supplier_account", "0");
                                                    transfer_resourcehost_to_idlemoduleObject.put("bk_biz_id", bk_biz_id);
                                                    transfer_resourcehost_to_idlemoduleObject.put("bk_host_id", bk_host_id_list);
                                                    //第三步资源池主机分配至业务的空闲机模块
                                                    JSONObject transfer_resourcehost_to_idlemodule_object = HttpsClient.httpRequest(apiUrlProperties.getTransfer_resourcehost_to_idlemodule_URL(), "POST", transfer_resourcehost_to_idlemoduleObject.toString());
//                                            if (!transfer_resourcehost_to_idlemodule_object.getBoolean("result")){
//                                                return transfer_resourcehost_to_idlemodule_object;
//                                            }

//                                            System.out.println("transferObject"+transferObject);

                                                    //第四步根据渠道的业务ID，集群ID，模块ID进行业务内主机转移
                                                    JSONObject transfer_host_module_object = HttpsClient.httpRequest(apiUrlProperties.getTransfer_host_module_URL(), "POST", transferObject.toString());
                                                    if (!transfer_host_module_object.getBoolean("result")) {
                                                        return transfer_host_module_object;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    JSONObject result = new JSONObject();
                                    result.put("result", false);
                                    result.put("code", 1);
                                    result.put("message", "第" + (i + 1) + "条数据不符合规范，请修改后再次导入！");
                                    result.put("data", "");
                                    return result;
                                }
                            }
                        }
                    }
                }
            }
        } else {
            JSONObject result = new JSONObject();
            result.put("result", false);
            result.put("code", 1);
            result.put("message", "查找实例主键信息失败，请重新操作");
            result.put("data", "查找实例主键信息失败，请重新操作");
            return result;
        }
        JSONObject resultJson = new JSONObject();
        resultJson.put("result", true);
        resultJson.put("code", 0);
        resultJson.put("message", "导入成功");
        resultJson.put("data", "导入成功");
        return resultJson;
    }

}
